package com.zy.ivideo.controller;

import com.zy.ivideo.entity.GiftEntity;
import com.zy.ivideo.token.PassToken;
import com.zy.ivideo.utils.ResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/gift")
@Api(value = "礼物模块API接口", description = "该模块提供礼物模块服务", tags = "礼物模块")
public class GiftController {

    @PassToken
    @ApiOperation(value = "获取礼物列表", notes = "获取礼物列表", httpMethod = "GET")
    @GetMapping("/getGiftList")
    public com.zy.ivideo.entity.ResponseEntity<List<GiftEntity>> getGiftList() {

        List<GiftEntity> list=new ArrayList<>();
        list.add(new GiftEntity(0,"angel","天使","",100));
        list.add(new GiftEntity(1,"halloween","万圣节","",200));
        list.add(new GiftEntity(2,"HamburgerArrow","汉堡","",100));
        list.add(new GiftEntity(3,"heartbeat","心跳","",100));
        list.add(new GiftEntity(4,"kingset","王冠","",200));
        list.add(new GiftEntity(5,"car","法拉利跑车","",500));
        list.add(new GiftEntity(6,"Rocket","火箭","",1000));
        list.add(new GiftEntity(7,"rose","玫瑰","",100));
        return ResponseUtils.success(list);

    }

    @PassToken
    @ApiOperation(value = "下载SVGA文件", notes = "下载SVGA文件", httpMethod = "GET")
    @GetMapping("/download/{filename}")
    public ResponseEntity<FileSystemResource> downloadFile(@PathVariable
                                                           @ApiParam(value = "下载的SVGA文件名", name = "filename", required = true)
                                                           String filename) throws  URISyntaxException {
        String rootPath = System.getProperty("user.dir");
        File file=new File(rootPath+ "/svga/" +filename+".svga");
        if(!file.exists()){
            return ResponseEntity.notFound().build();
        }
        FileSystemResource resource=new FileSystemResource(file);
        HttpHeaders headers=new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION,"attachment; filename="+filename);
        return ResponseEntity.ok().headers(headers).body(resource);
    }
}
